/*
 * joystick.c
 *
 * Created: 17.09.2012 10:01:00
 *  Author: vegardvo
 */ 

#include "adcDriver.h"
#include "inputDriver.h"
#include "definitions.h"
#include <avr/io.h>
#include <util/delay.h>

void joystickDriver_init(){
	CLEAR_BIT(DDRB,PB0);
	SET_BIT(PORTB,PB0);
}

struct joystickPos inputDriver_joystick_getPos()
{
	struct joystickPos pos;
	pos.x_pos = adcDriver_read(1);
	pos.y_pos = adcDriver_read(2);
	
	return pos;
}	

struct joystickDirection inputDriver_joystick_getDir()
{
	struct joystickDirection dir;
	
	struct joystickPos pos = inputDriver_joystick_getPos();
	
	if(pos.x_pos > UPPER_THRESHOLD)
	{
		dir.x_dir = RIGHT;
	} 
	else if (pos.x_pos < LOWER_THRESHOLD)
	{
		dir.x_dir = LEFT;
	}
	else
	{
		dir.x_dir = CENTER;
	}
	
	if(pos.y_pos > UPPER_THRESHOLD)
	{
		dir.y_dir = UP;
	}
	else if (pos.y_pos < LOWER_THRESHOLD)
	{
		dir.y_dir = DOWN;
	}
	else
	{
		dir.y_dir = CENTER;
	}
	
	return dir;
}
 
 struct sliderPos inputDriver_slider_getPos()
 {
	 struct sliderPos pos;
	 pos.L_pos = adcDriver_read(3);
	 pos.R_pos = adcDriver_read(4);
	 
	 return pos;
 }
 
 int getButton(enum buttons button)
 {
	 if (button == JOY)
	 {
		return !(READ_BIT(PINB,PB0));
	 }
	 else if (button == A)
	 {
		 if(READ_BIT(PINB,PB1))
		 {
			return TRUE;
		 } 
		 else
		 {
			return FALSE; 
		 }
	 }
	else if (button == B)
	{
		 if(READ_BIT(PINB,PB2))
		 {
			 return TRUE;
		 }
		 else
		 {
			 return FALSE;
		 }
	}
	else
	{ 
		return -1;
	}
 }